package com.alt.ui;

import java.io.InputStream;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alt.base.ALTTabActivity;
import com.alt.handler.CMCmdHandler;
import com.alt.manager.ALTAppMgr;
import com.alt.manager.ALTPTCMgr;
import com.alt.manager.ALTPTCMgr.SCREEN_UI;
import com.alt.models.TransactionData;
import com.alt.utils.ALTSysUtils;
import com.alt.R;
import com.facebook.widget.ProfilePictureView;

public class ALTCart extends ALTTabActivity implements OnClickListener {
	private TextView tvNameUser;
	private Button btnEdit;
	private ImageView imgAvartar;
	private TextView tvMoney;
	private RelativeLayout rlHistory;
	private RelativeLayout rlShare;
	private RelativeLayout rlInvite;
	private RelativeLayout rlInvitepage;
	private EditText edtUsername;
	private Button btnInvite;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_cart);
		onSetScreenMode(SCREEN_UI.Personal);
		tvNameUser = (TextView) findViewById(R.id.tvNameUser);
		btnEdit = (Button) findViewById(R.id.btnEdit);
		imgAvartar = (ImageView) findViewById(R.id.imgAvartar);
		tvMoney = (TextView) findViewById(R.id.tvMoney);
		rlHistory = (RelativeLayout) findViewById(R.id.rlHistory);
		rlShare = (RelativeLayout) findViewById(R.id.rlShare);
		rlInvite = (RelativeLayout) findViewById(R.id.rlInvite);
		rlInvitepage = (RelativeLayout) findViewById(R.id.rlInvitepage);

		edtUsername = (EditText) findViewById(R.id.edInvite);
		btnInvite = (Button) findViewById(R.id.btnInvite);

		rlInvite.setOnClickListener(this);
		rlShare.setOnClickListener(this);
		btnEdit.setOnClickListener(this);
		rlHistory.setOnClickListener(this);
		btnInvite.setOnClickListener(this);
//		 initData();
		

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}


	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
		ImageView bmImage;

		public DownloadImageTask(ImageView bmImage) {
			this.bmImage = bmImage;
		}

		protected Bitmap doInBackground(String... urls) {
			String urldisplay = urls[0];
			Bitmap mIcon11 = null;
			try {
				InputStream in = new java.net.URL(urldisplay).openStream();
				mIcon11 = BitmapFactory.decodeStream(in);
			} catch (Exception e) {
				Log.e("HAI", e.getMessage());
				e.printStackTrace();
			}
			return mIcon11;
		}
		

		protected void onPostExecute(Bitmap result) {
			bmImage.setImageBitmap(result);
		}
	}

	private void initData() {
		tvNameUser.setText(ALTAppMgr.getInstance().getUserLogInfo().Username);
		if (ALTAppMgr.isLoginFace) {
			Log.v("HAI", "set image face");
			new DownloadImageTask(imgAvartar).execute("http://graph.facebook.com/" + ALTAppMgr.getInstance().getUserLogInfoModel().getFacebookID() + "/picture");
			btnEdit.setVisibility(View.GONE);
			Log.v("HAI", "set image face done");
		} else {
			if (ALTAppMgr.isChangeAvarta) {
				imgAvartar.setImageBitmap(ALTSysUtils.byte2bitmap(ALTAppMgr.getInstance().getUserLogInfoModel().getImage()));
			} else {				
				if(ALTAppMgr.getInstance().getUserLogInfo().AvatarData.length!=0)
				imgAvartar.setImageBitmap(ALTSysUtils.byte2bitmap(ALTAppMgr.getInstance().getUserLogInfo().AvatarData));
			}
		}

		tvMoney.setText(getResources().getString(R.string.text_money) + " " + ALTAppMgr.getInstance().getUserLogInfo().TotalUserFee + " " + getResources().getString(R.string.text_money_value));

	}

	public void onClick(View v) {
		if (v == rlInvite) {
			ALTAppMgr.isBack = true;
			rlHistory.setVisibility(View.GONE);
			rlShare.setVisibility(View.GONE);
			rlInvite.setVisibility(View.GONE);
			rlInvitepage.setVisibility(View.VISIBLE);
		}
		if (v == rlShare) {

			onEventReceiver(SCREEN_UI.SHAREFRIEND);
		}
		if (v == btnEdit) {
			onEventReceiver(SCREEN_UI.EDITPROFILE);
		}
		if (v == rlHistory) {
			onEventReceiver(SCREEN_UI.TRANSACTION_HISTORY);
		}
		if (v == btnInvite) {
			Bundle b = new Bundle();
			b.putString("username", edtUsername.getText().toString());

			TransactionData trans = new TransactionData();
			trans.AdsId = "";
			trans.TransGroupId = ALTPTCMgr.TRANSACTION_TYPE.INVITE.toString();
			trans.UserId = ALTAppMgr.getInstance().getUserLogInfo().UserId;
			trans.UserFee = "200";
			trans.InvitedUserId = edtUsername.getText().toString();

			b.putSerializable(ALTPTCMgr.REST_OBJECT_PUT, trans);
			ALTAppMgr.getInstance().startServiceCmd(CMCmdHandler.COMMAND_UPDATE_TRANSACTION, new CMCmdHandler(), b, true);
		}
	}

	@Override
	public void processCommandResult(int commandCode, Object resultData) {
		// TODO Auto-generated method stub
		super.processCommandResult(commandCode, resultData);

		Bundle bResult = (Bundle) resultData;

		switch (commandCode) {

		case CMCmdHandler.COMMAND_UPDATE_TRANSACTION: {
			String result = bResult.getString(ALTPTCMgr.REST_OBJECT_RETURN);
			result = result.replace("\"", "");
			// Toast.makeText(this, result, Toast.LENGTH_LONG).show();
			if (result.equals("1")) {
				// update new total user fee
				ALTAppMgr.getInstance().getUserLogInfo().TotalUserFee = String.valueOf(Integer.parseInt(ALTAppMgr.getInstance().getUserLogInfo().TotalUserFee) + 200);
				edtUsername.setText("");
			}

			AlertDialog.Builder alert = new AlertDialog.Builder(this.getParent() == null ? this : this.getParent());
			alert.setMessage(result.equals("1") ? R.string.txt_update_success : R.string.txt_update_fail);
			alert.setPositiveButton(R.string.txt_btn_ok, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {

				}
			});
			alert.show();
		}
			break;

		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		rlHistory.setVisibility(View.VISIBLE);
		rlShare.setVisibility(View.VISIBLE);
		rlInvite.setVisibility(View.VISIBLE);
		rlInvitepage.setVisibility(View.GONE);
		super.onBackPressed();
	}

}
